package com.chen.blog.service.impl;

import com.chen.blog.mapper.CategoryMapper;
import com.chen.blog.pojo.Category;
import com.chen.blog.service.CategoryService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cgj
 * @date 1/19/25 10:34 AM
 */
@Service
@Transactional

public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    @Transactional(readOnly = true)
    public List<Category> findAllCategory() {
        return categoryMapper.selectAllCategory();
    }

    @Override
    @Transactional(readOnly = true)
    public Map findAllByPage(Integer pageIndex, Integer pageSize, Category category) {

//        开启分页
        Page<Object> page = PageHelper.startPage(pageIndex, pageSize);
//        查询
        HashMap map = new HashMap();
//        前端要多个数据,可以用map集合放回
        map.put("list",categoryMapper.selectAllBtPage(category));
        map.put("total",page.getTotal());
        return map;
    }

    @Override
    public Integer changeCategory(Category category) {
        return categoryMapper.updateCategory(category);
    }

    @Override
    public Integer addCategory(Category category) {
        return categoryMapper.insertCategory(category);
    }

    @Override
    public Integer removeCategory(Integer cid) {
       return categoryMapper.deleteCategory(cid);
    }
}
